package com.hgy.carsb.service;


import com.hgy.carsb.entity.ProfitTotalDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class ProfitTotalService {

    @Autowired
    private InfoService infoService;

    public ProfitTotalDetail getDetail(int year, int month, String code1, String code2, String prefix){
        String thisMonth = infoService.getRatio(year, month, code1, code2, prefix);
        String lastMonth;
        if (month==1){
            lastMonth = infoService.getRatio(year-1, 12, code1, code2, prefix);
        }else {
            lastMonth = infoService.getRatio(year, month-1, code1, code2, prefix);
        }
        BigDecimal thisYear = new BigDecimal(infoService.getRatio(year, 1, code1, code2, prefix));
        for (int i=2; i<=month; i++){
            thisYear = thisYear.add(new BigDecimal(infoService.getRatio(year, i, code1, code2, prefix)));
        }
        String thisYearSum = thisYear.toString();
        BigDecimal lastYear = new BigDecimal(infoService.getRatio(year-1, 1, code1, code2, prefix));
        for (int i=2; i<=12; i++){
            lastYear = lastYear.add(new BigDecimal(infoService.getRatio(year-1, i, code1, code2, prefix)));
        }
        String lastYearSum = lastYear.toString();
        return new ProfitTotalDetail(thisMonth, lastMonth, thisYearSum, lastYearSum);
    }
}
